package com.itheima.stock.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.itheima.stock.common.StockInConfig;
import com.itheima.stock.common.domain.*;
import com.itheima.stock.mapper.StockBlockRtInfoMapper;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockRtInfoMapper;
import com.itheima.stock.service.StockService;
import com.itheima.stock.mapper.StockBusinessMapper;
import com.itheima.stock.pojo.StockBusiness;
import com.itheima.stock.utils.DateTimeUtil;
import com.itheima.stock.vo.resp.R;
import com.itheima.stock.vo.resp.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("stockService")
@Slf4j
public class StockServiceImpl implements StockService {
    @Autowired
    private StockBusinessMapper stockBusinessMapper;

    @Autowired
    private StockInConfig stockInConfig;
    //股票大盘数据详情表
    @Autowired
    private StockMarketIndexInfoMapper stockMarketIndexInfoMapper;
    //    @Override
//    public R<List<StockBusiness>> getAllStockBusiness() {
//        List<StockBusiness> list= stockBusinessMapper.findAll();
//        return R.ok(list);
//    }
    @Autowired
    private StockBlockRtInfoMapper stockBlockRtInfoMapper;

    @Autowired
    private StockRtInfoMapper stockRtInfoMapper;

    @Override
    public R<List<StockBusiness>> getAllStockBusiness() {
        return null;
    }

    @Override
    public R<List<StockBusiness>> findAllBusiness() {
        List<StockBusiness> list = stockBusinessMapper.findAll();
        return R.ok(list);
    }

    //获取国内大盘的实时数据
    @Override
    public R<List<InnerMarketDomain>> innerIndexAll() {
        //1.获取国内大盘的id集合
        List<String> innerIds = stockInConfig.getInner();
        //2.获取最近最新的股票有效交易日
        String lastDate = DateTimeUtil.getLastDateString4Stock(DateTime.now());
        lastDate = "20211226105600";//TODO 后续大盘数据实时拉去,将改行注释掉
        //3.调用mapper查询指定日期下对应的国内大盘数据
        List<InnerMarketDomain> maps = stockMarketIndexInfoMapper.selectByIdAndDate(innerIds, lastDate);
        //组装响应的数据
        /*CollectionUtils.isNotEmpty() 集合包含null,size=0等多种情况
        而== null 只能用来判断是否为null*/
        if (CollectionUtils.isEmpty(maps)) {
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        return R.ok(maps);
    }

    @Override
    public R<List<SectorBlockDomain>> sectorAllLimit() {
        //调用mapper接口获取数据
        List<SectorBlockDomain> infos = stockBlockRtInfoMapper.sectorAllLimit();
        //组装数据
        if (CollectionUtils.isEmpty(infos)) {
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        return R.ok(infos);
    }

    @Override
    public R<List<StockIncreaseDomain>> stockIncreaseLimit() {
        List<StockIncreaseDomain> infos = stockRtInfoMapper.stockIncreaseLimit();
        if (CollectionUtils.isEmpty(infos)) {
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        return R.ok(infos);
    }

    @Override
    public R<PageResult<StockIncreaseDomain>> stockAllLimit(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<StockIncreaseDomain> infos = stockRtInfoMapper.stockAll();
        if (CollectionUtils.isEmpty(infos)) {
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        PageInfo<StockIncreaseDomain> pageInfo = new PageInfo<>(infos);
        PageResult<StockIncreaseDomain> pageResult = new PageResult<>(pageInfo);
        return R.ok(pageResult);
    }

    @Override
    public R<Map> getStockUpDownCount() {
        String avlDate = DateTimeUtil.getLastDateString4Stock(DateTime.now());
        avlDate = "20220107092500";
        List<Map> upCount = stockRtInfoMapper.getUpDownCount(avlDate, 1);
        List<Map> downCount = stockRtInfoMapper.getUpDownCount(avlDate, 0);
        HashMap<String, List<Map>> info = new HashMap<>();
        info.put("upList", upCount);
        info.put("downList", downCount);
        return R.ok(info);
    }

    @Override
    public void stockExport(HttpServletResponse response, Integer page, Integer pageSize) {
        try {
            //1.设置响应数据的类型:excel
            response.setContentType("application/vnd.ms-excel");
            //2.设置响应数据的编码格式
            response.setCharacterEncoding("utf-8");
            //3.设置默认的文件名称
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("stockRt", "UTF-8");
            //设置默认文件名称
            response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //4.分页查询股票数据
            PageHelper.startPage(page,pageSize);
            List<StockIncreaseDomain> infos = stockRtInfoMapper.stockAll();

            //5.导出
            EasyExcel.write(response.getOutputStream(),StockIncreaseDomain.class).sheet("股票数据").doWrite(infos);
        } catch (IOException e) {
            log.info("股票excel数据导出异常，当前页：{}，每页大小：{}，异常信息：{}",page,pageSize,e.getMessage());
        }
    }
}
